package cn.itcast.bos.web.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.TimeUnit;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.utils.Constants;
import cn.itcast.bos.web.action.common.BaseAction;
import cn.itcast.crm.domain.Customer;
import cn.itcast.sms.utils.MailUtils;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class CustomerAction extends BaseAction<Customer> {
	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	@Autowired
	@Qualifier("jmsQueueTemplate")
	private JmsTemplate jmsTemplate;
	@Action(value = "customer_sendSms")
	public String sendSms() throws UnsupportedEncodingException {

		String randomCode = RandomStringUtils.randomNumeric(4);

		ServletActionContext.getRequest().getSession().setAttribute(model.getTelephone(), randomCode);
		
		System.out.println("手机验证码是" + randomCode);

		final String msg = "尊敬的客户,本次的验证码是" + randomCode + ",服务电话是110";
		
		jmsTemplate.send("bos_sms",new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				MapMessage mapMessage=session.createMapMessage();
				mapMessage.setString("telephone", model.getTelephone());
				mapMessage.setString("msg", msg);
				return mapMessage;
			}
		});
		return NONE;

	}

	private String checkcode;

	public void setCheckcode(String checkcode) {
		this.checkcode = checkcode;
	}

	@Action(value = "customer_regiter", results = {
			@Result(name = "success", type = "redirect", location = "signup-success.html"),
			@Result(name = "input", type = "redirect", location = "signup.html") })

	public String customerSave() {

		String checkcodeSession = (String) ServletActionContext.getRequest().getSession().getAttribute(model.getTelephone());

		if (checkcodeSession == null || !checkcodeSession.equals(checkcode)) {
			System.out.println("验证码错误");
			System.out.println("session中的" + checkcodeSession);
			System.out.println("传递过来的" + checkcode);
			return INPUT;
		}

		WebClient.create(Constants.CRM_MANAGEMENT_URL+"/crm_management/services/customerService/customer")
				.type(MediaType.APPLICATION_JSON).post(model);
		
		String randomNumeric = RandomStringUtils.randomNumeric(32);
		redisTemplate.opsForValue().set(model.getTelephone(),randomNumeric,24,TimeUnit.HOURS);
		String contentAction="尊敬的客户,请于24小时之内点击邮箱链接地址进行注册激活 <br/><a href='"+MailUtils.activeUrl+"?telephone="+model.getTelephone()+"&randomNumeric="+randomNumeric+"'>速运邮箱绑定地址</a>";
		MailUtils.sendMail("速运快递", contentAction, model.getEmail());
		return SUCCESS;
	}
	 private String randomNumeric;
	
	 
	public void setRandomNumeric(String randomNumeric) {
		this.randomNumeric = randomNumeric;
	}

	@Action("customer_activeMail")
	public String activeMail() throws IOException{
		ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
		
		String active = redisTemplate.opsForValue().get(model.getTelephone());
		System.out.println(active);
		
		if(active==null || !active.equals(randomNumeric )){
			ServletActionContext.getResponse().getWriter().println("激活码错误,请重新注册");
		}else{
			
			Customer customer = WebClient.create(Constants.CRM_MANAGEMENT_URL+"/crm_management/services/customerService/customer/telephone/"+model.getTelephone()).accept(MediaType.APPLICATION_JSON).get(Customer.class);
			if(customer.getType()==null || customer.getType()!=1){
			WebClient.create(Constants.CRM_MANAGEMENT_URL+"/crm_management/services/customerService/customer/updateType/"+model.getTelephone()).get();
			ServletActionContext.getResponse().getWriter().println("邮箱已经绑定绑定成功");
			redisTemplate.delete(model.getTelephone());
			}else{
				ServletActionContext.getResponse().getWriter().println("邮箱已经绑定,无需重复激活");

			}
			
		}
		
		return NONE;
	}
	
	@Action(value="customer_login",results={@Result(name="login",type="redirect",location="login.html"),
											@Result(name="success",type="redirect",location="index.html#/myhome")})
	public String login(){
		Customer customer=WebClient.create(Constants.CRM_MANAGEMENT_URL+"/crm_management/services/customerService/customer/login?telephone="+model.getTelephone()+"&password="+model.getPassword()).accept(MediaType.APPLICATION_JSON).get(Customer.class);
		 
		if(customer==null){
			return LOGIN;
		}else{
			ServletActionContext.getRequest().getSession().setAttribute("customer", customer);
			
			return SUCCESS;
		}
		
		
	}
	
	
	
	
	
	
	
}
